package com.winz.cultureapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.ActivtiyOrderDesActivity;
import com.winz.cultureapp.adapter.xrcv.PersonalItem2Adapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.BookFixBean;
import com.winz.cultureapp.entity.EventBusBean;
import com.winz.cultureapp.entity.PersonItem2ListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.ActivityBookCancleReq;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 个人记录--活动预定
 */
public class PersonalItem2Fragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener, PersonalItem2Adapter.OnCancledListener {
    @BindView(R.id.xrcv_data)
    public XRecyclerView mXrcv_data;

    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;

    private List<PersonItem2ListBean.DataBean.ListBean> mDatas;
    private PersonalItem2Adapter mAdapter;

    private String title;


    public static PersonalItem2Fragment getInstance(String title) {
        PersonalItem2Fragment fragment = new PersonalItem2Fragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_personal_item2;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_data.setLayoutManager(manager);
        mXrcv_data.setPullRefreshEnabled(true);
        mXrcv_data.setLoadingMoreEnabled(false);
        mAdapter = new PersonalItem2Adapter(mContext, mDatas, R.layout.empty_view, R.layout.item_personal_item2);
    }

    @Override
    protected void initEvents() {

        mXrcv_data.setAdapter(mAdapter);
        mXrcv_data.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnCancleListener(this);
    }

    @Override
    protected void loadDatas() {
        getList();
    }

    @Override
    protected void getDatas(Bundle arguments) {
        title = arguments.getString("title");
    }

    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();
        HttpUitls.onGetByToken("activity_book_record", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN), RequestFactory.getInstance().unit_book_fix_record(page + "", limit + ""), new OnResponeListener<PersonItem2ListBean>() {
            @Override
            public void onSuccess(PersonItem2ListBean bean) {
                Logger.d("activity_book_record==>>", JsonUtil.getJsonFromObj(bean));

                HttpUitls.isExpire(bean.code);

                if (bean.code == 0) {
                    totalPage = bean.totalPage;
                    if (bean.data.list != null && bean.data.list.size() > 0) {
                        mXrcv_data.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = bean.data.list;
                        } else {
                            mDatas.addAll(bean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        PersonItem2ListBean.DataBean.ListBean empty = new PersonItem2ListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_data.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(bean.msg);
                }
                if (isRefresh) {
                    mXrcv_data.refreshComplete();
                } else {
                    mXrcv_data.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("activity_book_record==>>", error);
                if (isRefresh) {
                    mXrcv_data.refreshComplete();
                } else {
                    mXrcv_data.loadMoreComplete();
                }
            }
        });
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        loadDatas();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page >= totalPage) {
            mXrcv_data.setNoMore(true, true, "没有更多数据了");
        } else {
            page++;
            loadDatas();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.KEY.RECORD.RECORD_DATA, mDatas.get(position));
        startActivity(ActivtiyOrderDesActivity.class, false, bundle, 0);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getList();
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    /**
     * 取消预定
     *
     * @param position
     */
    @Override
    public void onCancle(int position) {
        showProgressDialog();
        HttpUitls.onPostByToken("activity_book_cancel",
                SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),
                new ActivityBookCancleReq(TextUtils.isEmpty(mDatas.get(position).activitySeatRecordId)?"":mDatas.get(position).activitySeatRecordId),
                new OnResponeListener<BookFixBean>() {
                    @Override
                    public void onSuccess(BookFixBean bean) {
                        Logger.d("activity_book_cancel==>>", JsonUtil.getJsonFromObj(bean));
                        HttpUitls.isExpire(bean.code);
                        if (bean.code == 0) {
                            showMsg("取消预定成功");
                            getList();
                        } else {
                            showMsg(bean.msg);
                        }

                        dismissProgressDialog();
                    }

                    @Override
                    public void onFailed(String error) {
                        dismissProgressDialog();
                        Logger.d("activity_book_cancel==>>", error);
                    }
                });
    }


    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void messageEventBus(EventBusBean<String> event){
        if (event.type == 34){
            if (event.action == 0){
                getList();
            }
        }

    }
}
